from mail import *
from record import *
from timetable import *

# 课表年份学期
term = ""  # TODO term, e.g. 1
year = ""  # TODO year, e.g. 2021-2022

# 数据获取模式，取值：email, db
mode = "email"  # TODO get user data from email

# 邮件
mail_username = ''  # TODO mail_username
mail_password = ''  # TODO mail_password
pop3_server = 'pop.163.com'  # 邮件服务器地址

# 获取所有的用户
source = []
if mode == "email":
    source = uoh_mail_service('uoh', mail_username, mail_password, pop3_server)
elif mode == "db":
    source = get_source()

# Chrome WebDriver
chrome = webdriver.Chrome()

# 获取课表
for student in source:
    chrome.get("http://10.10.10.71/default2.aspx")
    chrome.maximize_window()

    # 用户信息
    name, username, password = '', '', ''
    if mode == "email":
        name, username, password = student[1]
    elif mode == "db":
        name, username, password = student
    
    try:
        page_source = get_timetable_page(chrome, term, year, name, username, password)
        time_table = parse_timetable(page_source)

        # 处理存储数据显示
        for k, v in time_table.items():
            for i, c in enumerate(time_table[k]):
                if c is None:
                    continue  # 无课

                # TODO you can control the display of excel cells by modifying this
                time_table[k][i][0] = '{0}'.format(name)

        # 写入 excel
        write_in_excel('./{0}-{1}.xlsx'.format(year, term), time_table, sep='; ')

        print(name, "OK")
    except Exception as e:
        print(name, e)

chrome.close()
